/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.action.company;

import com.yfeng.common.utils.BeanUtil;
import com.yfeng.exception.ActionException;
import com.yfeng.goodjuhome.model.User;
import com.yfeng.goodjuhome.service.IUserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 企业用户后台
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-15
 * Time: 15:29:25
 */
@Controller("companyAdminAction")
public class CompanyAdminAction {

    private final Log log = LogFactory.getLog(this.getClass());

    @Autowired
    @Qualifier("userService")
    private IUserService userService;

    ////后台定位页面///
    /**
     * 定位到登录页面
     *
     * @return
     */
    @RequestMapping("/home/e/admin/login")
    public String toLoginPage() {
        return "/user/qy/admin/e_admin_login";
    }

    @RequestMapping("/home/e/admin/top")
    public String toTopPage() {
        return "/user/qy/admin/e_admin_top";
    }

    @RequestMapping("/home/e/admin/bottom")
    public String toBottomPage() {
        return "/user/qy/admin/e_admin_bottom";
    }

    @RequestMapping("/home/e/admin/left")
    public String toLeftPage() {
        return "/user/qy/admin/e_admin_left";
    }

    @RequestMapping("/home/e/admin/main")
    public String toMainPage() {
        return "/user/qy/admin/e_admin_main";
    }


    @RequestMapping("/home/e/admin/newsList")
    public String toNewsListPage() {
        return "/user/qy/admin/e_admin_newsList";
    }

     @RequestMapping("/home/e/admin/productList")
    public String toProductListPage() {
        return "/user/qy/admin/e_admin_productList";
    }

    @RequestMapping("/home/e/admin/profile")
    public String toProfilePage() {
        return "/user/qy/admin/e_admin_profile";
    }

    @RequestMapping("/home/e/admin/jobList")
    public String toJobListPage() {
        return "/user/qy/admin/e_admin_jobList";
    }

    @RequestMapping("/home/e/admin/contact")
    public String toContactPage() {
        return "/user/qy/admin/e_admin_contact";
    }


    /**
     * 定位到后台首页,先登录才能到这里
     *
     * @return
     */
    @RequestMapping("/home/e/admin/index")
    public String toIndexPage(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ActionException {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter write = response.getWriter();

            if (session.getAttribute("currUser") == null)
            {
                write.write("<script>alert('请先登录');history.back();</script>");
                return null;
            }

            
            User user = (User)BeanUtil.getBean("user");
            user.setUser((User)session.getAttribute("currUser"));
            if(!user.isCompanyUser())
                write.write("<script>alert('非法用户类型');history.back();</script>");

          //  write.write("<script>window.location='"+request.getContextPath()+"/user/qy/admin/e_admin_index.jsp';</script>");
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ActionException(e.getMessage(), e);
        }

        return "/user/qy/admin/e_admin_index";
    }
}
